<?php

/**
 * This is the model class for table "{{auditable_log}}".
 *
 * The followings are the available columns in table '{{auditable_log}}':
 * @property integer $id
 * @property integer $userId
 * @property integer $action
 * @property string $modelClass
 * @property integer $modelId
 * @property string $change
 * @property string $time
 *
 * The followings are the available model relations:
 * @property User $user
 */
class AuditableLog extends CActiveRecord {

	const ACTION_CREATE = 1;
	const ACTION_UPDATE = 2;
	const ACTION_DELETE = 3;

	/**
	 * Returns the static model of the specified AR class.
	 * @return AuditableLog the static model class
	 */
	public static function model($className = __CLASS__) {
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName() {
		return '{{auditable_log}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules() {
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array(
				'userId, action, modelId',
				'numerical',
				'integerOnly' => true
			),
			array(
				'modelClass',
				'length',
				'max' => 255
			),
			array(
				'change, time',
				'safe'
			),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array(
				'id, userId, action, modelClass, modelId, change, time',
				'safe',
				'on' => 'search'
			),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations() {
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'user' => array(
				self::BELONGS_TO,
				'User',
				'userId'
			),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels() {
		return array(
			'id' => 'ID',
			'userId' => 'User',
			'action' => 'Action',
			'modelClass' => 'Model Class',
			'modelId' => 'Model',
			'change' => 'Change',
			'time' => 'Time',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search() {
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria = new CDbCriteria;

		$criteria->compare('id', $this->id);
		$criteria->compare('userId', $this->userId);
		$criteria->compare('action', $this->action);
		$criteria->compare('modelClass', $this->modelClass, true);
		$criteria->compare('modelId', $this->modelId);
		$criteria->compare('change', $this->change, true);
		$criteria->compare('time', $this->time, true);

		return new CActiveDataProvider($this, array(
			'criteria' => $criteria,
		));
	}
}
